/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain menu_text_color copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gzwanchuang.xhgj.app;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;

import com.example.havi.helper.ChatHelper;
import com.gzwanchuang.architecture.utils.Utils;
import com.gzwanchuang.xhgj.BuildConfig;
import com.gzwanchuang.xhgj.help.HyphenateLibraryCallBackHandle;
import com.gzwanchuang.xhgj.help.UserHelper;
import com.gzwanchuang.xhgj.net.NetworkManager;
import com.gzwanchuang.xhgj.push.JPushHelper;
import com.tencent.ocr.sdk.common.OcrModeType;
import com.tencent.ocr.sdk.common.OcrSDKConfig;
import com.tencent.ocr.sdk.common.OcrSDKKit;
import com.tencent.ocr.sdk.common.OcrType;
//import com.helipay.mposlib.api.MPApi;

/**
 * Create by KunMinX at 19/10/29
 */
public class App extends Application implements ViewModelStoreOwner {

    //TODO tip：可借助 Application 来管理一个应用级 的 SharedViewModel，
    // 实现全应用范围内的 生命周期安全 且 事件源可追溯的 视图控制器 事件通知。

    public ViewModelStore mAppViewModelStore;

    public ViewModelProvider.Factory mFactory;
    private static ChatHelper chatHelper;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppViewModelStore = new ViewModelStore();
        NetworkManager.INSTANCE.init(this);
//        MPApi.INSTANCE.init(this, false);  // 收款sdk  false 初始化
        initJPush();
        Utils.init(this);
        initHyphenateIM();
        OcrModeType modeType = OcrModeType.OCR_DETECT_MANUAL; // 设置默认的业务识别模式自动 + 手动步骤模式
        OcrType ocrType = OcrType.BankCardOCR; // 设置默认的业务识别，银行卡
        OcrSDKConfig configBuilder = OcrSDKConfig.newBuilder("AKIDlRvCDySfcIhpiJdgTzECsIRJuFo67rLX" ,"xd2A2cwzkFTQ6VdU5PSFkyvgAOK0QZQ0", null)
                .ocrType(ocrType)
                .setModeType(modeType)
                .build();
// 初始化 SDK
        OcrSDKKit.getInstance().initWithConfig(this.getApplicationContext(), configBuilder);
    }

    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        return mAppViewModelStore;
    }



    private void initJPush() {
        JPushHelper.init(this);
        if (UserHelper.isLogin()) {
            JPushHelper.setAlias(UserHelper.getUser().getPush_id());
        }
    }


    public static ChatHelper getChatHelper(){
        return chatHelper;
    }

    public ViewModelProvider getAppViewModelProvider(Activity activity) {
        return new ViewModelProvider((App) activity.getApplicationContext(),
                ((App) activity.getApplicationContext()).getAppFactory(activity));
    }

    private ViewModelProvider.Factory getAppFactory(Activity activity) {
        Application application = checkApplication(activity);
        if (mFactory == null) {
            mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
        }
        return mFactory;
    }

    private Application checkApplication(Activity activity) {
        Application application = activity.getApplication();
        if (application == null) {
            throw new IllegalStateException("Your activity/fragment is not yet attached to "
                    + "Application. You can't request ViewModel before onCreate call.");
        }
        return application;
    }

    private Activity checkActivity(Fragment fragment) {
        Activity activity = fragment.getActivity();
        if (activity == null) {
            throw new IllegalStateException("Can't create ViewModelProvider for detached fragment");
        }
        return activity;
    }


    public String getServiceHost() {
        return serviceHost;
    }

    public void setServiceHost(String serviceHost) {
        this.serviceHost = serviceHost;
    }

    private String serviceHost = BuildConfig.SERVERHEAD;

    private void initHyphenateIM() {
        //初始化
        chatHelper = ChatHelper.getInstance();
        chatHelper.init(this,new HyphenateLibraryCallBackHandle());
        //同步用户账号与环信账号信息 项目使用中应该判断是否登录
    }

}
